home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Graphics / SPD / Sources / tetra.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-10  |  4.6 KB  |  173 lines  |  [TEXT/R*ch]

  1. /*
  2.  * tetra.c - Create a tetrahedral pyramid.  This environment is based on the
  3.  *    scene used by Glassner ("Space Subdivision for Fast Ray Tracing," IEEE
  4.  *    CG&A, October 1984) and Kay & Kajiya ("Ray Tracing Complex Scenes,"
  5.  *    SIGGRAPH '86 Proceedings) for testing their ray tracers.
  6.  *    One light source.
  7.  *
  8.  * Author:  Eric Haines, 3D/Eye, Inc.
  9.  *
  10.  * Note:  the view and light positions are the same (after transformation to
  11.  *    a different set of world coordinates) as used by Kay & Kajiya,
  12.  *    courtesy of Tim Kay.  For some reason, the number of shadow rays
  13.  *    generated is different (Kay gets 34K, I get 46K).  One light source.
  14.  *
  15.  * size_factor determines the number of polygons output.
  16.  *    Total triangular polygons = 4**SF
  17.  *
  18.  *    size_factor    # triangles
  19.  *         1             4
  20.  *         2            16
  21.  *         3            64
  22.  *
  23.  *         6          4096
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include <stdlib.h>    /* atoi */
  29. #include "def.h"
  30. #include "drv.h"    /* display_close() */
  31. #include "lib.h"
  32.  
  33. static    int    size_factor = 6 ;
  34. static int raytracer_format = OUTPUT_RT_DEFAULT;
  35. static int output_format    = OUTPUT_CURVES;
  36.  
  37. #ifdef OUTPUT_TO_FILE
  38. static FILE * stdout_file = NULL;
  39. #else
  40. #define stdout_file stdout
  41. #endif /* OUTPUT_TO_FILE */
  42.  
  43.  
  44. /* Create tetrahedrons recursively */
  45. void
  46. create_tetra( depth, center )
  47.     int depth ;
  48.     COORD4 center ;
  49. {
  50.     int num_face, num_vert, swap, vert_ord[3] ;
  51.     int x_dir, y_dir, z_dir ;
  52.     COORD3 face_pt[3], obj_pt[4] ;
  53.     COORD4 sub_center ;
  54.  
  55.     PLATFORM_MULTITASK();
  56.  
  57.     if ( depth <= 1 ) {
  58.     /* Output tetrahedron */
  59.  
  60.     /* find opposite corners of a cube which form a tetrahedron */
  61.     for ( num_vert = 0, x_dir = -1 ; x_dir <= 1 ; x_dir += 2 ) {
  62.         for ( y_dir = -1 ; y_dir <= 1 ; y_dir += 2 ) {
  63.         for ( z_dir = -1 ; z_dir <= 1 ; z_dir += 2 ) {
  64.             if ( x_dir*y_dir*z_dir == 1 ) {
  65.             obj_pt[num_vert][X] =
  66.                     center[X] + (double)x_dir * center[W] ;
  67.             obj_pt[num_vert][Y] =
  68.                     center[Y] + (double)y_dir * center[W] ;
  69.             obj_pt[num_vert][Z] =
  70.                     center[Z] + (double)z_dir * center[W] ;
  71.             ++num_vert ;
  72.             }
  73.         }
  74.         }
  75.     }
  76.  
  77.     /* find faces and output */
  78.     for ( num_face = 0 ; num_face < 4 ; ++num_face ) {
  79.         /* output order:
  80.          *   face 0:  points 0 1 2
  81.          *   face 1:  points 3 2 1
  82.          *   face 2:  points 2 3 0
  83.          *   face 3:  points 1 0 3
  84.          */
  85.         for ( num_vert = 0 ; num_vert < 3 ; ++num_vert ) {
  86.         vert_ord[num_vert] = (num_face + num_vert) % 4 ;
  87.         }
  88.         if ( num_face%2 == 1 ) {
  89.         swap = vert_ord[0] ;
  90.         vert_ord[0] = vert_ord[2] ;
  91.         vert_ord[2] = swap ;
  92.         }
  93.  
  94.         for ( num_vert = 0 ; num_vert < 3 ; ++num_vert ) {
  95.         COPY_COORD3( face_pt[num_vert], obj_pt[vert_ord[num_vert]] ) ;
  96.         }
  97.         lib_output_polygon( 3, face_pt ) ;
  98.     }
  99.     }
  100.  
  101.     else {
  102.     /* Create sub-tetrahedra */
  103.  
  104.     /* find opposite corners of a cube to form sub-tetrahedra */
  105.     for ( x_dir = -1 ; x_dir <= 1 ; x_dir += 2 ) {
  106.         for ( y_dir = -1 ; y_dir <= 1 ; y_dir += 2 ) {
  107.         for ( z_dir = -1 ; z_dir <= 1 ; z_dir += 2 ) {
  108.             if ( x_dir*y_dir*z_dir == 1 ) {
  109.             sub_center[X] =
  110.                 center[X] + (double)x_dir * center[W] / 2.0 ;
  111.             sub_center[Y] =
  112.                 center[Y] + (double)y_dir * center[W] / 2.0 ;
  113.             sub_center[Z] =
  114.                 center[Z] + (double)z_dir * center[W] / 2.0 ;
  115.             sub_center[W] = center[W] / 2.0 ;
  116.  
  117.             create_tetra( depth-1, sub_center ) ;
  118.             }
  119.         }
  120.         }
  121.     }
  122.     }
  123. }
  124.  
  125. int
  126. main(argc,argv)
  127.     int argc ;
  128.     char *argv[] ;
  129. {
  130.     COORD3  back_color, tetra_color, light ;
  131.     COORD3  from, at, up ;
  132.     COORD4  center_pt ;
  133.  
  134.     PLATFORM_INIT(SPD_TETRA);
  135.  
  136.     /* Start by defining which raytracer we will be using */
  137.     if ( lib_gen_get_opts( argc, argv,
  138.             &size_factor, &raytracer_format, &output_format ) ) {
  139.     return EXIT_FAIL;
  140.     }
  141.     if ( lib_open( raytracer_format, "Tetra.out" ) ) {
  142.     return EXIT_FAIL;
  143.     }
  144.  
  145.     /* output background color - UNC sky blue */
  146.     /* NOTE: Do this BEFORE lib_output_viewpoint(), for display_init() */
  147.     SET_COORD3( back_color, 0.078, 0.361, 0.753 ) ;
  148.     lib_output_background_color( back_color ) ;
  149.  
  150.     /* output viewpoint */
  151.     SET_COORD3( from, 1.022846, -3.177154, -2.174512 ) ;
  152.     SET_COORD3( at, -0.004103, -0.004103, 0.216539 ) ;
  153.     SET_COORD3( up, -0.816497, -0.816497, 0.816497 ) ;
  154.     lib_output_viewpoint( from, at, up, 45.0, 1.0, 1.0, 512, 512);
  155.  
  156.     /* output light source */
  157.     SET_COORD4( light, 1.876066, -18.123936, -5.000422, 1.0 ) ;
  158.     lib_output_light( light ) ;
  159.  
  160.     /* output tetrahedron color - red */
  161.     SET_COORD3( tetra_color, 1.0, 0.2, 0.2 ) ;
  162.     lib_output_color(NULL, tetra_color, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0);
  163.  
  164.     /* compute and output tetrahedral object */
  165.     SET_COORD4( center_pt, 0.0, 0.0, 0.0, 1.0 ) ;
  166.     create_tetra( size_factor, center_pt ) ;
  167.  
  168.     lib_close();
  169.  
  170.     PLATFORM_SHUTDOWN();
  171.     return EXIT_SUCCESS;
  172. }
  173.